package com.ymatou.iapi.productmanager.testcase;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.json.JSONArray;
import org.json.JSONObject;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.productmanager.service.SaveCatalogPackageListCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.AssertService;
import com.ymttest.database.annotation.SqlMapperFactory;
import com.ymttest.database.annotation.product.IntergratedProductMapper;
import com.ymttest.utils.MapUtil;

public class Ts_SaveCatalogPackageList {
	private static SaveCatalogPackageListCall saveCatalogPackageListCall = new SaveCatalogPackageListCall();
	private static IntergratedProductMapper intergratedProductMapper = SqlMapperFactory.getIntergratedProductMapper();
	
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("保存套装信息");
	}
	
	@Before
	public void caseUp() {
	}
	
	@AfterClass
	public static void tearDown() {
		Logger.generateResult(saveCatalogPackageListCall.getOpurl());
	}
	
	@After
	public void caseDown() {
		Logger.end();
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_SaveCatalogPackageList_001() {
		Logger.start(true,"保存一个套装信息");
		try{
			Map<String,Object> updateMap = MapUtil.hashMap("SubCatalogName","ceshi套装","CategoryId",1233,
														"SecondCategoryId",3123,"ThirdCategoryId",4123,
														"Price",20,"Num",30,"Sku","test","ProductCode","procodetest");
			
			JSONObject ret = saveCatalogPackageListCall.callAndGetReturnData(updateMapToReq(Arrays.asList(updateMap)));
			
			checkSubCatalogInfo(ret, Arrays.asList(updateMap));
			
		}catch(Exception e){
			
		}
	}
	
	
	
	

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_SaveCatalogPackageList_002() {
		Logger.start(true,"保存两个套装信息");
		try{
			Map<String,Object> updateMap = MapUtil.hashMap("SubCatalogName","ceshi套装","CategoryId",1233,
														"SecondCategoryId",3123,"ThirdCategoryId",4123,
														"Price",20,"Num",30,"Sku","test","ProductCode","procodetest");
			
			Map<String,Object> updateMap1 = MapUtil.hashMap("SubCatalogName","ceshi套11","CategoryId",1232,
														"SecondCategoryId",3122,"ThirdCategoryId",4122,
														"Price",2.1,"Num",301);
			
			JSONObject ret = saveCatalogPackageListCall.callAndGetReturnData(updateMapToReq(Arrays.asList(updateMap,updateMap1)));
			
			checkSubCatalogInfo(ret, Arrays.asList(updateMap,updateMap1));
			
		}catch(Exception e){
			
		}
	}
	
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_SaveCatalogPackageList_003() {
		Logger.start(true,"创建套装，name缺省");
		try{
			//缺名字
			Map<String,Object> updateMap = MapUtil.hashMap("SubCatalogName","","CategoryId",1233,
														"SecondCategoryId",3123,"ThirdCategoryId",4123,
														"Price",20,"Num",30,"Sku","test","ProductCode","procodetest");
			
			JSONObject ret = saveCatalogPackageListCall.callAndGetReturnData(updateMapToReq(Arrays.asList(updateMap)));
			
			Logger.verifyEquals(400, ret.getInt("Code"), "缺名字 应400");
			
		}catch(Exception e){
			
		}
	}
	
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_SaveCatalogPackageList_004() {
		Logger.start(true,"创建套装，CategoryId缺省");
		try{
			//缺CategoryId
			Map<String,Object> updateMap = MapUtil.hashMap("SubCatalogName","test","CategoryId",0,
														"SecondCategoryId",3123,"ThirdCategoryId",4123,
														"Price",20,"Num",30,"Sku","test","ProductCode","procodetest");
			
			JSONObject ret = saveCatalogPackageListCall.callAndGetReturnData(updateMapToReq(Arrays.asList(updateMap)));
			
			Logger.verifyEquals(400, ret.getInt("Code"), "缺CategoryId 应400");
			
		}catch(Exception e){
			
		}
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_SaveCatalogPackageList_005() {
		Logger.start(true,"创建套装，SecondCategoryId缺省");
		try{
			//缺SecondCategoryId
			Map<String,Object> updateMap = MapUtil.hashMap("SubCatalogName","test","CategoryId",1123,
														"SecondCategoryId",0,"ThirdCategoryId",0,
														"Price",20,"Num",30,"Sku","test","ProductCode","procodetest");
			
			JSONObject ret = saveCatalogPackageListCall.callAndGetReturnData(updateMapToReq(Arrays.asList(updateMap)));
			
			Logger.verifyEquals(400, ret.getInt("Code"), "缺SecondCategoryId 应400");
			
		}catch(Exception e){
			
		}
	}
	
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_SaveCatalogPackageList_006() {
		Logger.start(true,"创建套装，ThirdCategoryId缺省");
		try{
			//缺ThirdCategoryId
			Map<String,Object> updateMap = MapUtil.hashMap("SubCatalogName","test","CategoryId",123,
														"SecondCategoryId",3123,"ThirdCategoryId",0,
														"Price",20,"Num",30,"Sku","test","ProductCode","procodetest");
			
			JSONObject ret = saveCatalogPackageListCall.callAndGetReturnData(updateMapToReq(Arrays.asList(updateMap)));
			
			Logger.verifyEquals(400, ret.getInt("Code"), "缺ThirdCategoryId 应400");
			
		}catch(Exception e){
			
		}
	}
	
	
	
	
	
	
	
	
	
	
	public static void checkSubCatalogInfo(JSONObject ret,List<Map<String,Object>> updateMapList) throws Exception{
		Logger.verifyEquals(200, ret.getInt("Code"), "return code 200");
		
		JSONArray subId = ret.getJSONObject("Data").getJSONArray("SubCatalogIdList");
		Logger.verifyEquals(updateMapList.size(), subId.length(), "新建套装模板数量应该相同");
		
		
		for (int i=0;i<subId.length();i++){
			List<Map<String,Object>> retInSql = intergratedProductMapper.selectSubCatalogBySubCatalogId(subId.getInt(i))
											.stream().filter(x->(Integer)x.get("Action")==0).collect(Collectors.toList());
			
			Logger.verifyEquals(1, retInSql.size(), "套装记录数应为1，id:"+subId.getInt(i));
			
			AssertService.assertResultEqual(updateMapList.get(i), retInSql.get(0));
		}
	}
	
	
	
	public static Map<String,Object> updateMapToReq(List<Map<String,Object>> updateMapList){
		Map<String,Object> retMap =  new HashMap<>();
		
		for (int i=0;i<updateMapList.size();i++){
			String key = "ProductPackageList["+i+"]->";
			updateMapList.get(i).forEach((x,y)->{
				retMap.put(key+x, y);
			});
		}
		return retMap;
	}
	
	
}
